//
//  CFRMAddDishesController.swift
//  CrazyFatReducingMeal
//
//  Created by iOS-Czz on 2023/11/8.
//

import UIKit
import ProgressHUD
class CFRMAddDishesController: CFRMBaseViewController {

    @IBOutlet var pairedTV: UITextView!
    @IBOutlet var nutrationTV: UITextView!
    @IBOutlet var recommentTF: UITextField!
    @IBOutlet var nameTF: UITextField!
    var model:CFRMDishesModel? = nil
    var isEdit = false
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Recipe addition"
        self.nutrationTV.placeholder = "Please enter nutrition"
        self.nutrationTV.placeholderColor = .lightGray
        self.pairedTV.placeholder = "Please enter paired dishes"
        self.pairedTV.placeholderColor = .lightGray
        self.addRightBarButton(imageName: "icon_save")
        if self.isEdit {
            editInfo()
        }
        // Do any additional setup after loading the view.
    }
    
    func editInfo(){
        self.nameTF.text = self.model?.name
        self.recommentTF.text = self.model?.recommendation
        self.nutrationTV.text = self.model?.nutrition
        self.pairedTV.text = self.model?.paired
    }
    
    override func clockRightBarButtonAction() {
        self.view.endEditing(true)
        if self.nameTF.text!.isEmpty {
            showTips("Please enter recipe name")
            return
        }
        if self.recommentTF.text!.isEmpty {
            showTips("Please enter recommendation index")
            return
        }
        if self.nutrationTV.text!.isEmpty{
            showTips("Please enter nutrition")
            return
        }
        if self.pairedTV.text!.isEmpty {
            showTips("Please enter paired dishes")
            return
        }
        if !self.isEdit {
            self.model = CFRMDishesModel()
        }
        self.model?.name = self.nameTF.text!
        self.model?.recommendation = self.recommentTF.text!
        self.model?.nutrition = self.nutrationTV.text!
        self.model?.paired = self.pairedTV.text!
        if !self.isEdit{
            self.model?.isDefault = false
            self.model?.saveToDB()
            let helper = CFRMDishesModel.getUsingLKDBHelper()
            helper.delete(with: CFRMDishesModel.self, where: "isDefault = true")
        }else{
            self.model?.updateToDB()
        }
        ProgressHUD.showSuccess(self.isEdit ? "Recipe updated succeed" : "Recipe addition succeed")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    func showTips(_ message:String){
        ProgressHUD.showError(message)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
